package com.jh.hts_jdk.io.compress;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipWriter {
	private ZipOutputStream zout;

	public ZipWriter(File destFile) throws FileNotFoundException{
		zout = new ZipOutputStream(new FileOutputStream(destFile));
	}

	public void add(File file, String relativePath) throws FileNotFoundException, IOException{
		FileInputStream fin = new FileInputStream(file);
		ZipEntry entry = new ZipEntry(relativePath);
		zout.putNextEntry(entry);
		
		byte[] buffer = new byte[1024];
		int len;
		while ((len = fin.read(buffer)) > 0) {
			zout.write(buffer, 0, len);
		}
		
		zout.closeEntry();
		fin.close();
	}

	public void add(File file) throws FileNotFoundException, IOException{
		String relativePath = file.getName();
		add(file, relativePath);
	}

	public void close() throws IOException{
		zout.close();;
	}
}
